import { Tree } from 'antd'
import { DataNode, TreeProps } from 'antd/es/tree'
import React from 'react'

const mockTreeData: DataNode[] = [
  {
    title: 'parent 0',
    key: '0-0',
    children: [
      {
        title: 'leaf 0-0',
        key: '0-0-0',
        children: [
          {
            title: 'dsadsa',
            key: 'greatyar',
            isLeaf: true
          }
        ]
      },
      { title: 'leaf 0-1', key: '0-0-1', isLeaf: true }
    ]
  },
  {
    title: 'parent 1',
    key: '0-1',
    children: [
      { title: 'leaf 1-0', key: '0-1-0', isLeaf: true },
      { title: 'leaf 1-1', key: '0-1-1', isLeaf: true }
    ]
  }
]

const CommonTree = ({
  checkedKeys,
  onCheck,
  treeData,
  defaultExpandedKeys,
  disabled = false
}: {
  checkedKeys: Array<string>
  onCheck: (arr: Array<string>) => void
  treeData?: DataNode[]
  defaultExpandedKeys?: React.Key[]
  disabled?: boolean
}) => {
  const handleCheck: TreeProps['onCheck'] = (checkedKeys) => {
    onCheck(checkedKeys)
  }

  const finalTreeData = treeData || mockTreeData
  return (
    <div className="flex flex-col gap-3 border-warmGray-300 border-solid border-1 p1">
      <Tree
        disabled={disabled}
        height={600}
        checkable
        selectable={false}
        checkedKeys={checkedKeys}
        autoExpandParent
        onCheck={handleCheck}
        treeData={finalTreeData}
        defaultExpandedKeys={defaultExpandedKeys ?? finalTreeData.map((item) => item.key)}
      />
    </div>
  )
}

export default CommonTree
